package com.baobao.service;

import com.baobao.dao.AskleaveDAO;

import com.baobao.domain.Askleave;

import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Askleave entities
 * 
 */

@Service("AskleaveService")
@Transactional
public class AskleaveServiceImpl implements AskleaveService {

	/**
	 * DAO injected by Spring that manages Askleave entities
	 * 
	 */
	@Autowired
	private AskleaveDAO askleaveDAO;

	/**
	 * Instantiates a new AskleaveServiceImpl.
	 *
	 */
	public AskleaveServiceImpl() {
	}

	/**
	 */
	@Transactional
	public Askleave findAskleaveByPrimaryKey(String parentPhone, String studentId, Calendar applyTime) {
		return askleaveDAO.findAskleaveByPrimaryKey(parentPhone, studentId, applyTime);
	}

	/**
	 * Return a count of all Askleave entity
	 * 
	 */
	@Transactional
	public Integer countAskleaves() {
		return ((Long) askleaveDAO.createQuerySingleResult("select count(*) from Askleave o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Askleave entity
	 * 
	 */
	@Transactional
	public void deleteAskleave(Askleave askleave) {
		askleaveDAO.remove(askleave);
		askleaveDAO.flush();
	}

	/**
	 * Return all Askleave entity
	 * 
	 */
	@Transactional
	public List<Askleave> findAllAskleaves(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Askleave>(askleaveDAO.findAllAskleaves(startResult, maxRows));
	}

	/**
	 * Load an existing Askleave entity
	 * 
	 */
	@Transactional
	public Set<Askleave> loadAskleaves() {
		return askleaveDAO.findAllAskleaves();
	}

	/**
	 * Save an existing Askleave entity
	 * 
	 */
	@Transactional
	public void saveAskleave(Askleave askleave) {
		Askleave existingAskleave = askleaveDAO.findAskleaveByPrimaryKey(askleave.getParentPhone(), askleave.getStudentId(), askleave.getApplyTime());

		if (existingAskleave != null) {
			if (existingAskleave != askleave) {
				existingAskleave.setParentPhone(askleave.getParentPhone());
				existingAskleave.setStudentId(askleave.getStudentId());
				existingAskleave.setApplyTime(askleave.getApplyTime());
				existingAskleave.setStartTime(askleave.getStartTime());
				existingAskleave.setEndTime(askleave.getEndTime());
				existingAskleave.setReason(askleave.getReason());
				existingAskleave.setDetail(askleave.getDetail());
				existingAskleave.setTeacherPhone(askleave.getTeacherPhone());
				existingAskleave.setStatus(askleave.getStatus());
				existingAskleave.setApproveResult(askleave.getApproveResult());
				existingAskleave.setApproveTime(askleave.getApproveTime());
				existingAskleave.setApproveDetail(askleave.getApproveDetail());
			}
			askleave = askleaveDAO.store(existingAskleave);
		} else {
			askleave = askleaveDAO.store(askleave);
		}
		askleaveDAO.flush();
	}
}
